#coding=utf-8
import time  
from selenium import webdriver  
import re
import tkinter as tk

driver = webdriver.Chrome(r'C:\Users\guosu\Desktop\Justins\python\chromedriver.exe')  #打开浏览器
upperlimit_gold = 400
lowerlimit_gold = 390
upperlimit_sliver = 4350
lowerlimit_sliver = 4250  

def batch_check_interest_rate(driver):
        
    
    elem = driver.get('https://finance.sina.com.cn/futures/quotes/AUTD.shtml')  #打开网址
    information = "" #把结果要保存的变量先初始化好

    def get_info():   #定义主要方法： 复制利率到information变量，并同时打印出来 
        nonlocal information #调用父方法的变量 
        status = driver.page_source #把全网页源代码文本保存在status变量里 
        price = status[status.find('real-price price'):status.find('change-wrap')] #找到第二个/tr后的内容，就是说提取前两行信息的意思
        filtered_service_log = re.findall('">(.*?)</',price) 
        #print('The latest rate is: \n')
        information = filtered_service_log[0]
        #print('\n')  #前后空好行 

    # 正式开始执行查询近期rate任务
    #print('for US:\n')

    get_info()
    print(information)  #根据需要打印出利率详情统计。

    def popup():
        root = tk.Tk()
        root.wm_attributes('-topmost',1)
        root.title("价格提醒")
        root.geometry('500x300')
        label = tk.Label(root, text='Gold price is:\n'+information)
        label.pack()
        root.mainloop()
        
    if float(information) > upperlimit_gold or float(information) < lowerlimit_gold:
        popup()

    
    elem = driver.get('https://finance.sina.com.cn/futures/quotes/AGTD.shtml')
    information = "" #把结果要保存的变量先初始化好
    get_info()
    print(information)  #根据需要打印出利率详情统计。
    
    if float(information) > upperlimit_sliver or float(information) < lowerlimit_sliver:
        popup()

while True:
    batch_check_interest_rate(driver)
    time.sleep(300)
